package com.atm.thirdplt.service.impl;

import com.atm.thirdplt.common.constant.MongoCollConstants;
import com.atm.thirdplt.common.core.domain.entity.SysCtlInf;
import com.atm.thirdplt.service.IScheduleSyncControllerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * @ClassName ScheduleSyncControllerServiceImpl
 * @Description TODO
 * @Author wenyf
 * @Date 2021/2/3 15:30
 * @Version 1.0
 **/
@Service
public class ScheduleSyncControllerServiceImpl implements IScheduleSyncControllerService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void uptSysCtlValue(String code, String value) {

        Query query = new Query();
        query.addCriteria(Criteria.where("code").is(code));

        Update update = new Update();
        update.set("value", value);

        /**
         * 数据库有，就新增， 没有，就修改
         */
        mongoTemplate.upsert(query, update, SysCtlInf.class, MongoCollConstants.SYS_CTL_CONFIG);
    }

    @Override
    public String selSysCtlValue(String code) {

        Query query = new Query();
        query.addCriteria(Criteria.where("code").is(code));

        SysCtlInf ctlInf = mongoTemplate.findOne(query, SysCtlInf.class, MongoCollConstants.SYS_CTL_CONFIG);
        if (ctlInf != null){
            return ctlInf.getValue();
        }
        return null;
    }
}
